﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace TaskManage
{
    public partial class FormTaskView : Form
    {
        public string uid;
        public string uname;
        public string upriority;

        public FormTaskView()
        {
            InitializeComponent();
        }

        private void FormTaskView_Load(object sender, EventArgs e)
        {
            this.comboBox1.Items.Add("所有任务");
            this.comboBox1.Items.Add("我已完成的任务");
            this.comboBox1.Items.Add("我未完成的任务");
            if (upriority[0] < '4')
                this.comboBox1.Items.Add("我发布的任务");
        }

        private void btnSearch_Click(object sender, EventArgs e)
        {
            string qstr;
            DBConnect dbConn = new DBConnect();
            DataSet ds;
            DataTable dt;
            this.dataGridView1.Rows.Clear(); 
            this.dataGridView1.DataSource = null;
            switch (this.comboBox1.SelectedIndex)
            {
                case 0:
                    qstr = "SELECT task_id,task_name,task_pubTime,task_pubId,task_revId,task_staTime,task_endTime,task_revBg FROM tasks_info";
                    ds = dbConn.Query(qstr);
                    if (ds == null) break;
                    dt = ds.Tables[0];
                    for (int i = 0; i < dt.Rows.Count; i++)
                    {
                        DataRow irow = dt.Rows[i];
                        int idx = this.dataGridView1.Rows.Add();
                        
                        for (int j = 0; j < dt.Columns.Count-1; j++)
                        {
                            this.dataGridView1.Rows[idx].Cells[j].Value = irow[j].ToString();
                        }
                        this.dataGridView1.Rows[idx].Cells[3].Value = this.getNameById(irow[3].ToString());
                        this.dataGridView1.Rows[idx].Cells[4].Value = this.getNameById(irow[4].ToString());
                        if (irow[7].ToString() == "")
                            this.dataGridView1.Rows[idx].Cells[7].Value = "未完成";
                        else
                            this.dataGridView1.Rows[idx].Cells[7].Value = "已完成";
                    }
                    break;
                case 3:
                    qstr = "SELECT task_id,task_name,task_pubTime,task_pubId,task_revId,task_staTime,task_endTime,task_revBg FROM tasks_info WHERE task_pubId='" + uid + "'";
                    ds = dbConn.Query(qstr);
                    if (ds == null) break;
                    dt = ds.Tables[0];
                    for (int i = 0; i < dt.Rows.Count; i++)
                    {
                        DataRow irow = dt.Rows[i];
                        int idx = this.dataGridView1.Rows.Add();
                        
                        for (int j = 0; j < dt.Columns.Count-1; j++)
                        {
                            this.dataGridView1.Rows[idx].Cells[j].Value = irow[j].ToString();
                        }
                        this.dataGridView1.Rows[idx].Cells[3].Value = this.getNameById(irow[3].ToString());
                        this.dataGridView1.Rows[idx].Cells[4].Value = this.getNameById(irow[4].ToString());
                        if (irow[7].ToString() == "")
                            this.dataGridView1.Rows[idx].Cells[7].Value = "未完成";
                        else
                            this.dataGridView1.Rows[idx].Cells[7].Value = "已完成";
                    }
                    break;
                case 1:
                    qstr = "SELECT task_id,task_name,task_pubTime,task_pubId,task_revId,task_staTime,task_endTime,task_revBg FROM tasks_info WHERE task_revId='" +
                        uid + "' AND task_revBg <> ''";
                    ds = dbConn.Query(qstr);
                    if (ds == null) break;
                    dt = ds.Tables[0];
                    for (int i = 0; i < dt.Rows.Count; i++)
                    {
                        DataRow irow = dt.Rows[i];
                        int idx = this.dataGridView1.Rows.Add();
                        
                        for (int j = 0; j < dt.Columns.Count-1; j++)
                        {
                            this.dataGridView1.Rows[idx].Cells[j].Value = irow[j].ToString();
                        }
                        this.dataGridView1.Rows[idx].Cells[3].Value = this.getNameById(irow[3].ToString());
                        this.dataGridView1.Rows[idx].Cells[4].Value = this.getNameById(irow[4].ToString());
                        if (irow[7].ToString() == "")
                            this.dataGridView1.Rows[idx].Cells[7].Value = "未完成";
                        else
                            this.dataGridView1.Rows[idx].Cells[7].Value = "已完成";
                    }
                    break;
                case 2:
                    qstr = "SELECT task_id,task_name,task_pubTime,task_pubId,task_revId,task_staTime,task_endTime,task_revBg FROM tasks_info WHERE task_revId='" +
                        uid + "' AND task_revBg = ''";
                    ds = dbConn.Query(qstr);
                    if (ds == null) break;
                    dt = ds.Tables[0];
                    for (int i = 0; i < dt.Rows.Count; i++)
                    {
                        DataRow irow = dt.Rows[i];
                        int idx = this.dataGridView1.Rows.Add();
                        
                        for (int j = 0; j < dt.Columns.Count-1; j++)
                        {
                            this.dataGridView1.Rows[idx].Cells[j].Value = irow[j].ToString();
                        }
                        this.dataGridView1.Rows[idx].Cells[3].Value = this.getNameById(irow[3].ToString());
                        this.dataGridView1.Rows[idx].Cells[4].Value = this.getNameById(irow[4].ToString());
                        if (irow[7].ToString() == "")
                            this.dataGridView1.Rows[idx].Cells[7].Value = "未完成";
                        else
                            this.dataGridView1.Rows[idx].Cells[7].Value = "已完成";
                    }
                    break;
                default:
                    qstr = "SELECT task_id,task_name,task_pubTime,task_pubId,task_revId,task_staTime,task_endTime,task_revBg FROM tasks_info";
                    ds = dbConn.Query(qstr);
                    if (ds == null) break;
                    dt = ds.Tables[0];
                    for (int i = 0; i < dt.Rows.Count; i++)
                    {
                        DataRow irow = dt.Rows[i];
                        int idx = this.dataGridView1.Rows.Add();
                        
                        for (int j = 0; j < dt.Columns.Count-1; j++)
                        {
                            this.dataGridView1.Rows[idx].Cells[j].Value = irow[j].ToString();
                        }
                        this.dataGridView1.Rows[idx].Cells[3].Value = this.getNameById(irow[3].ToString());
                        this.dataGridView1.Rows[idx].Cells[4].Value = this.getNameById(irow[4].ToString());
                        if (irow[7].ToString() == "")
                            this.dataGridView1.Rows[idx].Cells[7].Value = "未完成";
                        else
                            this.dataGridView1.Rows[idx].Cells[7].Value = "已完成";
                    }
                    break;
            }
        }

        private string getNameById(string id)
        {
            string name;
            DBConnect dbConn = new DBConnect();
            DataSet ds = dbConn.Query("SELECT uname FROM users_info WHERE uid = '" + id + "'");
            DataTable dt = ds.Tables[0];
            DataRow row = dt.Rows[0];
            name = row[0].ToString();
            return name;
        }

        private string getIdByName(string name)
        {
            string id;
            DBConnect dbConn = new DBConnect();
            DataSet ds = dbConn.Query("SELECT uid FROM users_info WHERE uname = '" + name + "'");
            DataTable dt = ds.Tables[0];
            DataRow row = dt.Rows[0];
            id = row[0].ToString();
            return id;
        }
    }
}
